<template>
	<view id="app">
		<view class="container df fdc">
			<view class="ptb20 bgw f28 c666 pl20">
				身证份正面照
			</view>
			<view class="ver bgw pb20" v-if="idcard.is_auth=='1'">
				<image :src="idcard.identity_card_before"  mode="widthFix" style="width: 420rpx;height: 280rpx;"></image>
			</view>
			<view class="ver bgw pb20" v-else>
				<uv-upload :fileList="fileList1" name="1" :deletable="false" :maxCount="1" width="420rpx" height="280rpx" @afterRead="afterRead">
					<image :src="idcard.identity_card_before"  mode="widthFix" style="width: 420rpx;height: 280rpx;" ></image>
				</uv-upload>
			</view>
			<view class="ptb20 bgw f28 c666 pl20">
				身证份反面照
			</view>
			<view class="ver bgw pb20" v-if="idcard.is_auth=='1'">
				<image :src="idcard.identity_card_after"  mode="widthFix" style="width: 420rpx;height: 280rpx;"></image>
				
			</view>
			<view class="ver bgw pb20" v-else>
				<uv-upload :fileList="fileList2" name="2" :deletable="false" :maxCount="1" width="420rpx" height="280rpx"  @afterRead="afterRead">
					<image :src="idcard.identity_card_after"  mode="widthFix" style="width: 420rpx;height: 280rpx;"></image>
				</uv-upload>
			</view>
			<view class="rows bgw ptb20 bdb1s brcce">
				<view class="w150 f28 c666 pl20">姓名</view>
				<view class="f1">
					<uv-input placeholder="请输入姓名" border="none" v-model="idcard.name"></uv-input>
				</view>
			</view>
			<view class="rows bgw ptb20 bdb1s brcce">
				<view class="w150 f28 c666 pl20">身份证号</view>
					<view class="f1">
						<uv-input placeholder="请输入身份证号" border="none" v-model="idcard.identity_card"></uv-input>
					</view>
			</view>
			<view class="df fdc aic mt20" v-if="idcard.is_auth=='1'">
				<view class="f30 ptb20 cinfo">已实名认证</view>
			</view>
			<view class="df fdc aic mt20" v-else>
				<view class="btn">
					<uv-button color="#145cfe" iconColor="#fff" @click="submitIdcard()" text="提交"></uv-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	let that;
	export default {
		data() {
			return {
				fileList1:[],
				fileList2:[],
				idcard:{
					name:"",
					identity_card:"",
					identity_card_before:"",
					identity_card_after:"",
					is_auth:0,
				}
			}
		},
		onLoad(){
			that = this;
			this.getIdcard();
		},
		methods: {
			getIdcard:function(e){
				this.request({
					url:"/api/user/auth",
					success:function(rlt){
						if(rlt.code == 200){
							that.idcard = rlt.data;
						}
					}
				})
			},
			async afterRead (event){
				console.log(event);
				let file =event.file
				this[`fileList${event.name}`][0] = {
					...file,
					status: 'uploading',
					message: '上传中'
				}
				let res = await this.getToken(file.url);
				const result = await this.uploadfile(file.url,res)
				let item = this[`fileList${event.name}`][0]
				this[`fileList${event.name}`].splice(0, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: result
				}))
			},
			submitIdcard:function(e){
				if(this.fileList1.length == 0){
					this.util.alert('请上传身份证正面');
					return false;
				}
				if(this.fileList2.length == 0){
					this.util.alert('请上传身份证背面');
					return false;
				}
				if(this.idcard.name ==''){
					this.util.alert('请填写身份证姓名');
					return false;
				}
				if(this.idcard.identity_card ==''){
					this.util.alert('请填写身份证号码');
					return false;
				}
				this.idcard.identity_card_before = this.fileList1[0].url;
				this.idcard.identity_card_after = this.fileList2[0].url;
				this.request({
					url:"/api/user/subauth",
					data:this.idcard,
					success:function(rlt){
						if(rlt.code == 200){
							// 弹窗提示
							that.alert('提交成功')
							// 延时执行
							setTimeout(() => {
								uni.navigateBack()
							}, 500);
						}
					}
				})
			},
			uploadfile:function(url,res){
				return new Promise((resolve, reject) => {
					let imgurl = '';
					this.qnUpload.upload(url,function(rlt){
						imgurl = rlt.imageURL
						
					},function(e){
					},{
						region: 'NCN',
						uptoken: res.data.token,
						key:res.data.filename,
					});
					setTimeout(() => {
						resolve(res.data.cdnurl+imgurl);
					}, 1000)
				})
			},
			getToken(e){
				console.log(e);
				return this.request({url:"/api/common/qntoken",data:{'file':e}});
			},
			
		}
	}
</script>

<style lang="less" scoped>
.btn{width:480rpx;}
</style>
